package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LemonlaActorMapper;
import org.linlinjava.litemall.db.dao.LitemallGoodsMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LemonlaActorService {
    LemonlaActor.Column[] columns = new LemonlaActor.Column[]{LemonlaActor.Column.image1,LemonlaActor.Column.id, LemonlaActor.Column.name, LemonlaActor.Column.avatar, LemonlaActor.Column.categoryId, LemonlaActor.Column.addTime, LemonlaActor.Column.gender, LemonlaActor.Column.backgroundimage, LemonlaActor.Column.birthday,LemonlaActor.Column.mainimage,LemonlaActor.Column.nickname,LemonlaActor.Column.character,LemonlaActor.Column.descriptionParams,LemonlaActor.Column.type,LemonlaActor.Column.createUserId};
    LemonlaActor.Column[] columns_des = new LemonlaActor.Column[]{LemonlaActor.Column.image1,LemonlaActor.Column.description,LemonlaActor.Column.id, LemonlaActor.Column.name, LemonlaActor.Column.avatar, LemonlaActor.Column.categoryId, LemonlaActor.Column.addTime, LemonlaActor.Column.gender, LemonlaActor.Column.backgroundimage, LemonlaActor.Column.birthday,LemonlaActor.Column.mainimage,LemonlaActor.Column.nickname,LemonlaActor.Column.character,LemonlaActor.Column.descriptionParams,LemonlaActor.Column.type};

    @Resource
    private LemonlaActorMapper actorMapper;

    public List<LemonlaActor> queryByCategory(List<Integer> catList, int offset, int limit) {
        LemonlaActorExample example = new LemonlaActorExample();
        example.or().andCategoryIdIn(catList).andStatusEqualTo((byte) 0).andDeletedEqualTo(false);
        example.setOrderByClause("add_time  desc");
        PageHelper.startPage(offset, limit);

        return actorMapper.selectByExampleSelective(example, columns);
    }

    public List<LemonlaActor> queryByCategory(Integer catId, int offset, int limit) {
        LemonlaActorExample example = new LemonlaActorExample();
        example.or().andCategoryIdEqualTo(catId).andStatusEqualTo((byte) 0).andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return actorMapper.selectByExampleSelective(example, columns);
    }

    public LemonlaActor findById(Integer id) {
        LemonlaActorExample example = new LemonlaActorExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return actorMapper.selectOneByExampleSelective(example,columns);
    }

    public int add(LemonlaActor actor) {
        actor.setAddTime(LocalDateTime.now());
        actor.setUpdateTime(LocalDateTime.now());
        return actorMapper.insertSelective(actor);
    }

    public int update(LemonlaActor actor) {
        actor.setUpdateTime(LocalDateTime.now());
        return actorMapper.updateByPrimaryKeySelective(actor);
    }


    public LemonlaActor findByIdWithDes(Integer actorId) {
        LemonlaActorExample example = new LemonlaActorExample();
        example.or().andIdEqualTo(actorId).andDeletedEqualTo(false);
        return actorMapper.selectOneByExampleSelective(example,columns_des);
    }

    public List<LemonlaActor> findbyUserId(Integer userId) {
        LemonlaActorExample example = new LemonlaActorExample();
        example.or().andCreateUserIdEqualTo(userId).andDeletedEqualTo(false);
        return actorMapper.selectByExampleSelective(example,columns);
    }

    public List<LemonlaActor> querySelective(Integer actorId, String name, String nickname, Integer page, Integer size, String sort, String order) {
        LemonlaActorExample example = new LemonlaActorExample();
        LemonlaActorExample.Criteria criteria = example.createCriteria();

        if (actorId != null) {
            criteria.andIdEqualTo(actorId);
        }

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(nickname)) {
            criteria.andNicknameLike("%" + nickname + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return actorMapper.selectByExample(example);
    }


    public void deleteById(Integer id) {
        actorMapper.logicalDeleteByPrimaryKey(id);
    }
}
